<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>userment Config</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<link rel="stylesheet" href="../css/index-ui.css">
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
</head>
<body onload="on_refresh_user_content()">
	<div id="user_settings" style="padding:5px;padding-left:1px;padding-right:0px">
		<div style="float:right;padding-right:0px">
			<a class="easyui-linkbutton" style="width:80px" onclick="on_delete_user()">Delete</a>
		</div>
		<div style="float:right;padding-right:10px">
			<a class="easyui-linkbutton" style="width:80px" onclick="on_modify_user()">Modify</a>
		</div>
		<div style="float:right;padding-right:10px">
			<a class="easyui-linkbutton" style="width:80px" onclick="on_add_user()">Add</a>
		</div>
	</div>
	<br/><br/>
	<div id="user_man_layout_main" class="easyui-layout cls_rcp_layout_main">
		<div data-options="region:'center',split:true" title="" style="width:400px;">
			<table id="user_management_list_grid" class="easyui-datagrid" title="" style="width:100%;height:90%" pagination="false">
				<thead>
					<tr>
					    <th data-options="field:'id',hidden:true">id</th>
						<th data-options="field:'name',width:150">Name</th>
						<th data-options="field:'pw',width:150,align:'left'">PassWord</th>
						<th data-options="field:'level',width:80,align:'left'">Level</th>
						<th data-options="field:'desc',width:400,align:'left'">Desc</th>
						<th data-options="field:'uid',width:50,align:'left',hidden:true">uid</th>
					</tr>
				</thead>
			</table>
			<div id="user_win" class="easyui-window" title="" data-options="modal:true, closed:true" style="width:280px;height:300px;">
	    	<form style="padding:20px 20px 10px 40px;">
	    		<h2>userment information:</h2>
					<div style="margin:20px 20px 20px 20px;"></div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_user_name" data-options="prompt:'Name...'" style="width:180px;height:24px;"></p>
					</div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_user_pw" type="passWord" data-options="prompt:'PassWord...'" style="width:180px;height:24px;"></p>
					</div>
					<div style="margin-bottom:10px">
						<!-- <p><input class="easyui-textbox" id="text_user_level" data-options="prompt:'Level...'" style="width:180px;height:24px;"></p> -->
						<span>Level：</span>
						<!-- <select class="easyui-combobox" id="nLevel" name="state" panelHeight="auto"> -->
						<select class="easyui-combobox" id="combo_user_level" name="state" style="width:135px" panelHeight="auto" editable="false">
							<option value="1">Operator</option>
							<option value="2">Engineer</option>
							<option value="3">Administrator</option>
						</select>
					</div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_user_desc" data-options="prompt:'Desc...'" style="width:180px;height:24px;"></p>
					</div>
	    		<div style="padding:10px;text-align:center;">
	    			<a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="do_user_man_ok()">Ok</a>
	    			<a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="do_user_man_cancel()">Cancel</a>
	    		</div>
	    	</form>
	    </div>
		</div>
	</div>
</body>

<script language="javascript"> 


function on_refresh_user_content()
{
	$('#user_management_list_grid').datalist('loadData',[])

	$('#user_management_list_grid').datagrid({
		rownumbers:'true',
		singleSelect:'true',
		lines:true,
		clickToEdit: false,
		dblclickToEdit: false,
		loadMsg: 'Loading Data ......',
		method:'get'
	});
	if (ams_is_sim_mode()) {
		$.getJSON('user_01.json', function(json_data){
			$('#user_management_list_grid').datagrid('loadData',json_data.rows);
		})
	}else{
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getUserDatas"),	
			data:JSON.stringify({}),
			dataType: 'json',
			success : function(datas) {
				// console.log(getData())
				console.log(JSON.parse(datas.d))
				var vUserInfo = JSON.parse(datas.d);
				for(var i = 0; i < vUserInfo.rows.length; i++)
				{
					vUserInfo.rows[i].pw = "******";
				}
				$('#user_management_list_grid').datagrid('loadData',vUserInfo);
				//$('#user_management_list_grid').datagrid('loadData',JSON.parse(datas.d));
				
			},
			error: function (error) {
				console.log(error) 
			}
		});
	}
}

function on_add_user()
{
	if (checkUserLevel() == false){
		return;
	}
	
	$('#user_win').window({title:'Add'}).window('open');
}

function on_modify_user()
{
	if (checkUserLevel() == false){
		return;
	}
			
	var row = $("#user_management_list_grid").datagrid('getSelected')
	if (!row) {
		//alert("Select a user please.")
		$.messager.alert("Tips", "Select a user please..", "info");
		return
	}
	$('#user_win').window({title:'Modify'}).window('open');
	$('#text_user_name').textbox('setValue', row.name);
	$('#text_user_pw').textbox('setValue', row.passWord);
	// $('#text_user_level').textbox('setValue', row.level);
	$('#combo_user_level').combobox('select', row.level);
	$('#text_user_desc').textbox('setValue', row.desc);
}

function on_delete_user()
{
	if (checkUserLevel() == false){
		return;
	}
	
	var row = $("#user_management_list_grid").datagrid('getSelected')
	if (!row) {
		//alert("Select a userment please.")
		$.messager.alert("Tips", "Select a user please.", "info");
		return
	}
	else
	{
		// alert('delete userment: ' + row.id)
		console.log(row)

		$.messager.confirm('Confirm', 'Are you sure you want to delete it?', function(r){
			if (r){
				if (ams_is_sim_mode()) {
					//alert("Delete successful.");
					$.messager.alert("Tips", "Delete successful.", "info");
				}else{
					var userLogin = GetCookie('ams_user_name');
					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("cmdUserData"),	
						data:JSON.stringify({
							"user":userLogin,
							"cmd":3,
							"id":row.id,
							"name":"",
							"pw":"",
							"level":0,
							"desc":""
						}),
						dataType: 'json',
						success : function(datas) {
							// console.log(getData())
							console.log(datas.d)
							if(datas.d == "0"){
								//alert("Delete successful.");
								$.messager.alert("Tips", "Delete successful.", "info");
								on_refresh_user_content()
							}else if(datas.d == "-1"){
								//alert("Delete failed.");
								$.messager.alert("Tips", "Delete failed.", "info");
							}
						},
						error: function (error) {
							//alert(errorThrown.message);
							console.log(error) 
						}
					});
				}
			}
		});
	}
}
function do_user_man_ok()
{
	var row = $("#user_management_list_grid").datagrid('getSelected')
	console.log(row)
	var opts = $('#user_win').panel('options');
	var title = opts.title;//»ñÈ¡titleÊôÐÔ
	$('#user_win').window('close')
	var name = $("#text_user_name").textbox('getValue');
	var pw = $("#text_user_pw").textbox('getValue');
	// var nLevel = $("#nLevel").textbox('getValue');
	var nLevel = $("#combo_user_level").textbox('getValue');
	var desc = $("#text_user_desc").textbox('getValue');
	if (title == "Add")
	{
		if (ams_is_sim_mode()) {
			//alert("Add success.");
			$.messager.alert("Tips", "Add success.", "info");
		}else{
			var userLogin = GetCookie('ams_user_name');
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdUserData"),	
				data:JSON.stringify({
					"user":userLogin,
					"cmd":1,
					"id":"",
					"name":name,
					"pw":pw,
					"level":nLevel,
					"desc":desc
				}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						//alert("Add success.");
						$.messager.alert("Tips", "Add success.", "info");
						on_refresh_user_content()
					}else if(datas.d == "-1"){
						//alert("Add failure.");
						$.messager.alert("Tips", "Add failed.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	}
	else if (title == "Modify")
	{
		if (ams_is_sim_mode()) {
			//alert("Modified success.");
			$.messager.alert("Tips", "Modified success.", "info");
		}else{
			var userLogin = GetCookie('ams_user_name');
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdUserData"),	
				data:JSON.stringify({
					"user":userLogin,
					"cmd":2,
					"id":row.id,
					"name":name,
					"pw":pw,
					"desc":desc,
					"level":nLevel,
				}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						//alert("Modified success.");
						$.messager.alert("Tips", "Modified success.", "info");
						on_refresh_user_content()
					}else if(datas.d == "-1"){
						//alert("Modification failed.");
						$.messager.alert("Tips", "Modification failed.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	}
	else
	{
		var selVal = $('#user_group_list_grid').datalist('getSelected');
		alert(selVal['uid']);
	}
}

function do_user_man_cancel()
{
	$('#user_win').window('close')
}

</script>